package com.gxks.lhs.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxks.lhs.constants.Constants;
import com.gxks.lhs.constants.R;
import com.gxks.lhs.entity.Report;
import com.gxks.lhs.security.SecurityUtils;
import com.gxks.lhs.service.IReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.gxks.lhs.constants.Constants.REPORT_DEAL_WITH;

/**
 * <p>
 * 举报表 前端控制器
 * </p>
 *
 * @author LuoHaisong
 * @since 2023-05-19
 */
@Api(tags = "举报管理模块")
@Slf4j
@RestController
@RequestMapping("/report")
public class ReportController {

    @Autowired
    private IReportService reportService;

    @ApiOperation("根据id获取举报信息")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Integer id) {
        Report report = reportService.getById(id);
        if (report == null) {
            return R.error("暂无数据");
        }
        return R.success(report, "查询成功");
    }

    @ApiOperation("查询所有的举报信息")
    @GetMapping("/allList")
    public R list(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                  @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        Page<Report> list = reportService.findAllReports(current, size);
        if (list.getTotal() == 0) {
            return R.error("暂无数据");
        }
        return R.success(list, "查询成功");
    }

    @ApiOperation("根据条件查询举报列表")
    @PostMapping("/list")
    public R list(@RequestBody(required = false) Report report) {
        Page<Report> list = reportService.findReport(report, Constants.CURRENT_PAGE, Constants.PAGE_SIZE);
        if (list.getTotal() == 0) {
            return R.error("暂无数据");
        }
        return R.success(list, "查询成功");
    }

    @ApiOperation("处理举报")
    @PostMapping("/handle")
    public R handle(@RequestBody Report report) {
        if (report.getId() == null) {
            return R.error("请传入举报id");
        }
        if (report.getReportResult() == null) {
            return R.error("请传入处理结果");
        }
        String adminId = SecurityUtils.getCurrentUserId();
        report.setHandlerId(adminId);
        boolean b = reportService.handlingReport(report.getId(), REPORT_DEAL_WITH, adminId, report.getReportResult());
        if (b) {
            return R.success();
        }
        return R.error("处理失败");
    }
}

